package com.vertx.config;

import com.vertx.util.Runner;
import io.vertx.config.ConfigRetriever;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;

public class ConfigVerticle extends AbstractVerticle {


	public static void main(String[] args) {

		Runner.runExample(ConfigVerticle.class);

	}


	@Override
	public void start(Promise<Void> startPromise) throws Exception {

		System.setProperty("vertx-config-path", "conf\\config-dev.json");

		ConfigRetriever retriever = ConfigRetriever.create(vertx);

		retriever.getConfig(json -> {
			JsonObject jsonObject = null;
			if (json.failed()) {

			} else {
				jsonObject = json.result();

				System.out.println(jsonObject.getValue("c"));
			}

		});

		String s = (String) this.getConfig().result().getValue("c");

		System.out.printf("s:" +s );

//
//		this.getConfig().onSuccess(x -> {
//
//			System.out.println("c :" + x.getValue("c"));
//		});

	}

	private Future<JsonObject> getConfig(){
		Promise<JsonObject> promise = Promise.promise();
		ConfigRetriever retriever = ConfigRetriever.create(vertx);
		retriever.getConfig(json -> {
			if(json.succeeded()){
				promise.complete(json.result());
			}else{
				System.out.println("faild :" + json.cause().getMessage());
			}
		});
		return promise.future();
	}

}
